package com.boot2.core.log;

import com.boot2.core.aop.AbstractAop;
import com.boot2.core.model.AbstractLog;
import lombok.Data;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
* @author zhangweilin
* @date 2020/6/1 22:39
* @Description:  系统用户日志开关控制
*/
@Data
@CommonsLog
public class UserLogBeanDefinitionProcessor implements BeanPostProcessor {

    /**
     * 设置日志的po model类,默认系统自带的UserLog
     */
    private Class<? extends AbstractLog> logModelClass;

    /**
     * 是否开启系统日志持久化,默认为true
     */
    private boolean enable;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof AbstractAop) {
            AbstractAop abstractAop = (AbstractAop) bean;
            if (enable) {
                //如果为打开状态，设置为指定的model po
                abstractAop.enableUserLog(logModelClass);
            } else {
                //如果为关闭状态，只要设置为null,就表示不记录系统日志,此时指定的model po会被忽略
                abstractAop.enableUserLog(null);
            }
        }
        return bean;
    }
}